home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / keyb / keybuf21.zip / KEYBUF.ASM < prev    next >
Assembly Source File  |  1994-01-20  |  4KB  |  114 lines

  1. BufLen    equ    256        ; New keyboard buffer lenght
  2. StdLen    equ    32        ; Standard buffer lenght
  3. BVSeg    equ    0040h        ; BIOS Variables segment
  4. MaxSeg    equ    1030h        ; Highest suitable segment (read KeyBuf.doc)
  5. DosOwn    equ    0008h        ; DOS memory block signature
  6. MCBOwn    equ    0001h        ; Memory Control Bock owner field
  7. MCBNam    equ    0008h        ; Memory Control Block name field (DOS 4.0+)
  8.  
  9. ; We recalculate the BVS as it was 0000h because it's simples to load it!
  10. ; I.e., if Head was 0040:1A, now Head is 0000:041A.
  11. BIOSVar    segment    byte at 0    ; BVS
  12.     org    041Ah
  13. Head    dw    ?        ; Head pointer
  14. Tail    dw    ?        ; Tail pointer
  15.     org    0480h
  16. Bot    dw    ?        ; Starting offset of the buffer
  17. Top    dw    ?        ; Offset of the 1st word outside the buffer
  18. BIOSVar    ends
  19.  
  20. KeyBuf    segment    byte
  21.     assume    CS:KeyBuf, SS:KeyBuf, DS:KeyBuf, ES:nothing
  22.  
  23.     org    002Ch        ; Program environment segment in the PSP
  24. envseg    dw    ?        ; structure.
  25.  
  26.     org    0100h
  27. main:                ; Program (.COM) entry point
  28.     mov    DX,offset greet
  29.     mov    AH,09h
  30.     int    21h        ; Greeting message printout
  31.  
  32.     xor    AX,AX        ; First we see if KeyBuf was already loaded
  33.     mov    ES,AX        ; by calculating the buffer lenght.
  34.     mov    AX,ES:Top    ; The lenght is Top-Bot, of course.
  35.     sub    AX,ES:Bot    ; If we find the usual lenght (32 bytes)
  36.     cmp    AX,StdLen    ; we assume that we really need to run KeyBuf.
  37.     jbe    first        ; We assume otherwise that the buffer is big
  38.     mov    DX,offset uslss    ; enough abd that KeyBuf is useless.
  39.     mov    AL,01h        ; By doing so we detect if KeyBuf has already
  40.     jmp    exit        ; run once.
  41.  
  42. first:
  43.     mov    AX,envseg    ; First we disallocate the environment
  44.     mov    ES,AX        ; segment. If this segment is big enough
  45.     mov    AH,49h        ; the subsequent allocation call will
  46.     int    21h        ; get it again. If we want to assemble KeyBuf
  47.     xor    AX,AX        ; as a device driver, we have to remember that
  48.     mov    envseg,AX    ; there's no environment. Never tried.
  49.  
  50.     mov    AH,48h        ; Now we allocate a 256 bytes (128 2-bytes
  51.     mov    BX,BufLen/16    ; keystrokes) memory block for our new buffer.
  52.     int    21h
  53.     jnc    allocok        ; If allocation fails, we set the allocated
  54.     mov    AX,65535    ; segment to FFFF.
  55.  
  56. allocok:
  57.     cmp    AX,MaxSeg    ; Finally we check if the segment in AX is
  58.     jb    segok        ; lower enough in memory to be useful.
  59.     mov    DX,offset toohi    ; Here we use the `trick' with FFFF!
  60.     mov    AL,02h
  61.     jmp    exit
  62.  
  63. segok:
  64.     mov    DX,AX        ; Each memory block allocated has a prepended
  65.     dec    DX        ; memory control clock. It's 16 bytes long and
  66.     mov    ES,DX        ; has a filed for the owner of the block.
  67.     mov    DI,MCBOwn    ; We set the buffer as owned by DOS, so when we
  68.     mov    DX,DosOwn    ; will exit the program, the memory block will
  69.     mov    ES:[DI],DX    ; remain "resident".
  70.     mov    DI,MCBNam    ; Then we copy the string 'KeyBuf' into a field
  71.     mov    SI,offset greet ; of the memory control block used to store the
  72.     mov    CX,4        ; the name of the program that allocated it.
  73.     rep    movsw        ; This field has a meaning only for DOS 4.0+ .
  74.  
  75.     mov    CL,4        ; With simple calculations we translate
  76.     shl    AX,CL        ; our segment into an offset relatively
  77.     sub    AX,BVSeg*16    ; to the BVS. (seg*16) = (BVSeg*16)+off
  78.     mov    DX,AX        ; --> off = (seg*16)-(BVSeg*16)
  79.     add    DX,BufLen
  80.  
  81.     push    DS        ; We'll use DS to address the BVS to be
  82.     xor    CX,CX        ; as fast as we can while in the disabled
  83.     mov    DS,CX        ; interrupts section below.
  84.     assume    DS:BIOSVar
  85.  
  86.     cli            ; And now ... take the breath!
  87.     mov    Head,AX        ; While juggling with the BVS values
  88.     mov    Tail,AX        ; we have to avoid BIOS to modify those
  89.     mov    Bot,AX        ; values or we could get some incoherency.
  90.     mov    Top,DX
  91.     sti            ; Weew! You can breath normally, now!
  92.  
  93.     assume    DS:KeyBuf
  94.     pop    DS
  95.     mov    DX,offset allok    ; Successful message printout and exit
  96.     mov    AL,CL        ; with ERRORLEVEL set to 0.
  97.  
  98. exit:
  99.     mov    AH,09h
  100.     int    21h
  101.     mov    AH,4Ch        ; MS-DOG exit() service (AL = ERRORLEVEL)
  102.     int    21h
  103.  
  104. greet    db    'KeyBuf',0,0,    ; This is a dirty trick!
  105.                 '2.10  (c) 1992-1994 by Shq`n Soft',13,10,'KeyBuf : ','$'
  106. uslss    db    'useless',13,10,'$'
  107. toohi    db    'all memory below 1030:0 is busy',13,10,'$'
  108. allok    db    'successfully installed',13,10,'$'
  109.  
  110. KeyBuf    ends
  111.  
  112. end    main
  113. ; That's all, folks!
  114.